package com.lyq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyq.mapper.AdminMapper;
import com.lyq.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyq
 * @since 2020-04-23
 */
@Controller
@RequestMapping("/admin")
public class AdminContentController {
    @Autowired
    private AdminMapper adminMapper;

    @RequestMapping("/login")
    public String login(
            Admin admin,
            Model model,
            HttpSession session
    ){
        System.out.println(admin);
        // 查询名字密码存在的admin对象
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_name",admin.getAdminName());
        wrapper.eq("admin_password",admin.getAdminPassword());
        Admin admin1 = adminMapper.selectOne(wrapper);
        if(!StringUtils.isEmpty(admin1)){
          session.setAttribute("adminName",admin1.getAdminName());
            return "redirect:/main.html";
        }else{

            //登录失败！存放错误信息
            model.addAttribute("msg","用户名或密码错误");
            return "index";
        }
    }

    /**
     * 无条件查询管理员
     * @param model
     * @return
     */
    @RequestMapping("/adminList")
    public String adminList(Model model){
        List<Admin> adminList = adminMapper.selectList(null);
        model.addAttribute("list",adminList);
        return "admin/list";
    }


    @GetMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
    @GetMapping("/toAddAdmin")
    public String toAddAdmin(){
        return "admin/addadmin";
    }
    @PostMapping("/addAdmin")
        public String addAdmin(Admin admin){
            adminMapper.insert(admin);
            return "redirect:/admin/adminList";
        }
        @GetMapping("/toUpdateAdmin/{adminId}")
    public String toUpdateAdmin(@PathVariable("adminId")Integer adminId,Model model){
            Admin admin = adminMapper.selectById(adminId);

            model.addAttribute("admin",admin);

            return "admin/updateadmin.html";
        }
        @PostMapping("/updateAdmin")
    public String updateAdmin(Admin admin){
            System.out.println(admin);
            int update = adminMapper.updateById(admin);
            if(StringUtils.isEmpty(update)){
                System.out.println("插入成功");
            }
            return "redirect:/admin/adminList";
        }
    @GetMapping("/deleteAdmin/{adminId}")
    public String deleteAdmin(@PathVariable("adminId")Integer adminId){
        int i = adminMapper.deleteById(adminId);

        return "redirect:/admin/adminList";
    }
    }




